import 'package:flutter/material.dart';
import 'package:habit_master/core/models/habit.dart';
import 'package:habit_master/widgets/habit_card.dart';
import 'package:intl/intl.dart';

class HomeScreen extends StatefulWidget {
  const HomeScreen({Key? key}) : super(key: key);
  
  @override
  _HomeScreenState createState() => _HomeScreenState();
}

class _HomeScreenState extends State<HomeScreen> {
  // 模拟数据
  final List<Habit> _todayHabits = [
    // 这里将来会从数据服务中获取真实数据
  ];
  
  double get _completionPercentage {
    if (_todayHabits.isEmpty) return 0;
    int completed = _todayHabits.where((h) => h.isCompletedToday()).length;
    return completed / _todayHabits.length;
  }
  
  String _getTodayString() {
    final now = DateTime.now();
    final formatter = DateFormat('yyyy年MM月dd日，E', 'zh_CN');
    return formatter.format(now);
  }
  
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Column(
        children: [
          _buildHeader(),
          _buildProgressCard(),
          Expanded(
            child: _buildHabitsList(),
          ),
        ],
      ),
    );
  }
  
  Widget _buildHeader() {
    return Container(
      padding: const EdgeInsets.fromLTRB(20, 60, 20, 20),
      color: Theme.of(context).primaryColor,
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.center,
        children: [
          Text(
            _getTodayString(),
            style: const TextStyle(
              color: Colors.white,
              fontSize: 16,
              fontWeight: FontWeight.w400,
            ),
          ),
          const SizedBox(height: 5),
          const Text(
            '今日习惯',
            style: TextStyle(
              color: Colors.white,
              fontSize: 24,
              fontWeight: FontWeight.bold,
            ),
          ),
          const SizedBox(height: 5),
          const Text(
            '连续坚持：15天',
            style: TextStyle(
              color: Colors.white,
              fontSize: 14,
              fontWeight: FontWeight.w400,
            ),
          ),
        ],
      ),
    );
  }
  
  Widget _buildProgressCard() {
    return Container(
      margin: const EdgeInsets.fromLTRB(15, -15, 15, 0),
      padding: const EdgeInsets.all(20),
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.circular(15),
        boxShadow: [
          BoxShadow(
            color: Colors.black.withOpacity(0.1),
            blurRadius: 10,
            offset: const Offset(0, 5),
          ),
        ],
      ),
      child: Column(
        children: [
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              const Text(
                '今日完成',
                style: TextStyle(
                  fontWeight: FontWeight.bold,
                  fontSize: 16,
                ),
              ),
              Text(
                '${(_completionPercentage * 100).toInt()}%',
                style: TextStyle(
                  fontWeight: FontWeight.bold,
                  fontSize: 16,
                  color: Theme.of(context).primaryColor,
                ),
              ),
            ],
          ),
          const SizedBox(height: 10),
          ClipRRect(
            borderRadius: BorderRadius.circular(4),
            child: LinearProgressIndicator(
              value: _completionPercentage,
              backgroundColor: Colors.grey.shade200,
              valueColor: AlwaysStoppedAnimation<Color>(
                Theme.of(context).primaryColor,
              ),
              minHeight: 8,
            ),
          ),
        ],
      ),
    );
  }
  
  Widget _buildHabitsList() {
    return ListView(
      padding: const EdgeInsets.all(20),
      children: [
        const Text(
          '今日习惯',
          style: TextStyle(
            fontSize: 18,
            fontWeight: FontWeight.bold,
          ),
        ),
        const SizedBox(height: 15),
        if (_todayHabits.isEmpty)
          _buildEmptyState()
        else
          ...List.generate(
            _todayHabits.length,
            (index) => Padding(
              padding: const EdgeInsets.only(bottom: 15),
              child: HabitCard(
                habit: _todayHabits[index],
                onTap: () {
                  // 打开习惯详情
                },
                onToggle: (value) {
                  // 切换习惯完成状态
                },
              ),
            ),
          ),
        const SizedBox(height: 20),
        _buildMotivationalQuote(),
      ],
    );
  }
  
  Widget _buildEmptyState() {
    return Center(
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          const SizedBox(height: 40),
          Icon(
            Icons.add_task,
            size: 80,
            color: Colors.grey.shade300,
          ),
          const SizedBox(height: 20),
          Text(
            '今天没有需要完成的习惯',
            style: TextStyle(
              fontSize: 16,
              color: Colors.grey.shade600,
            ),
          ),
          const SizedBox(height: 10),
          ElevatedButton(
            onPressed: () {
              // 导航到添加习惯页面
            },
            child: const Text('添加新习惯'),
          ),
        ],
      ),
    );
  }
  
  Widget _buildMotivationalQuote() {
    return Container(
      padding: const EdgeInsets.all(15),
      decoration: BoxDecoration(
        color: const Color(0xFFF1F8E9),
        borderRadius: BorderRadius.circular(10),
      ),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: const [
          Text(
            '"习惯的力量远远超过我们的想象。好习惯给我们带来成功，坏习惯引我们走向失败。"',
            style: TextStyle(
              color: Color(0xFF2E7D32),
              fontStyle: FontStyle.italic,
            ),
          ),
          SizedBox(height: 5),
          Align(
            alignment: Alignment.centerRight,
            child: Text(
              '—— 詹姆斯·克利尔《掌控习惯》',
              style: TextStyle(
                color: Color(0xFF2E7D32),
                fontSize: 13,
              ),
            ),
          ),
        ],
      ),
    );
  }
}
